package com.zut.hospital.service.examination;

import com.alibaba.fastjson.JSON;
import com.zut.hospital.dao.examination.*;
import com.zut.hospital.entity.examination.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @PackageName:com.zut.hostest.service
 * @Date:2019/5/21 14:46
 * @Author: zsy
 * @Decription: 包含方法：查找所有结果列表，所有学院，所有已添加体检学生，体检学生计数，体检学院计数.
 */
@Service
public class PhySearchServiceImpl implements PhySearchService {

    @Autowired
    StudentMapper studentMapper;
    @Autowired
    DepartMapper departMapper;
    @Autowired
    ProjectMapper projectMapper;
    @Autowired
    Stu_Pro_RelationMapper stuProRelationMapper;
    @Autowired
    ResultMapper resultMapper;
    @Autowired
    PhyexamService phyexamService;

    public Map mapResult(Result result) {
        Map<String, String> resultMap = new HashMap<>();
        Stu_Pro_RelationExample stuProRelationExample;
        StudentExample studentExample;
        DepartExample departExample;
        ProjectExample projectExample;

        resultMap.put("id", result.getId().toString());

        stuProRelationExample = new Stu_Pro_RelationExample();
        stuProRelationExample.createCriteria().andIdEqualTo(result.getPhyRelationId());
        Integer stuId = stuProRelationMapper.selectByExample(stuProRelationExample).get(0).getRelatStuId();

        studentExample = new StudentExample();
        studentExample.createCriteria().andIdEqualTo(stuId);
        Student student = studentMapper.selectByExample(studentExample).get(0);

        resultMap.put("num", student.getStuNum());
        resultMap.put("stuName", student.getStuName());
        resultMap.put("age", student.getStuAge().toString());
        Integer sex = student.getStuSex();
        if (sex == 1)
            resultMap.put("sex", "男");
        else
            resultMap.put("sex", "女");
        resultMap.put("contact", student.getStuContactInfo());

        departExample = new DepartExample();
        departExample.createCriteria().andIdEqualTo(student.getStuDepartId());
        resultMap.put("deptName", departMapper.selectByExample(departExample).get(0).getDepName());

        projectExample = new ProjectExample();
        projectExample.createCriteria().andIdEqualTo(stuProRelationMapper.selectByExample(stuProRelationExample).get(0).getRelatProId());
        resultMap.put("proName", projectMapper.selectByExample(projectExample).get(0).getProName());
        resultMap.put("result", result.getPhyResult());
        resultMap.put("qualify", result.getPhyQualify());
        resultMap.put("remark", result.getPhyRemark());

        return resultMap;
    }

    @Override
    public String pageResult(int currPage, int pageSize) {
        ResultExample resultExample = new ResultExample();
        List<Result> results = resultMapper.selectByExample(resultExample);
        int size = results.size();

        List<Map<String, String>> resultList = new ArrayList<>();

        int firstIndex = (currPage - 1) * pageSize;
        int lastIndex = currPage * pageSize;
        if (currPage * pageSize > size) {
            lastIndex = size;
        }
        for (Result result : results.subList(firstIndex, lastIndex)) {
            resultList.add(mapResult(result));
        }
        phyexamService.listLessRepeat(resultList);
        return phyexamService.layuiJsonParse(resultList, size);
    }

    @Override
    public String findAllDept() {
        DepartExample departExample = new DepartExample();
        List<Depart> departs = departMapper.selectByExample(departExample);

        Map<String, String> resultMap;
        List<Map<String, String>> resultList = new ArrayList<>();

        for (Depart depart : departs) {
            resultMap = new HashMap<>();
            resultMap.put("deptId", depart.getId().toString());
            resultMap.put("deptName", depart.getDepName());
            resultList.add(resultMap);
        }
        return JSON.toJSONString(resultList);
    }

    @Override
    public String NonProDept() {
        /*
         * 查找所有还未添加体检的学院
         * 1. 通过关联表找到所有已经添加过项目的学生
         * 2. 通过每一个学生找到该学生所属学院
         * 3. 在所有学院中判断是否有该学院
         * 4. 学院列表中，没有的学院则表示还没有添加项目
         * */
        Map<String, String> resultMap;
        List<Map<String, String>> resultList = new ArrayList<>();

        DepartExample departExample;
        StudentExample studentExample;

        DepartExample departExample1 = new DepartExample();
        List<Depart> departList = departMapper.selectByExample(departExample1);

        Stu_Pro_RelationExample stu_pro_relationExample = new Stu_Pro_RelationExample();
        List<Stu_Pro_Relation> stu_pro_relations = stuProRelationMapper.selectByExample(stu_pro_relationExample);
        for (Stu_Pro_Relation stu_pro_relation : stu_pro_relations) {
            studentExample = new StudentExample();
            studentExample.or().andIdEqualTo(stu_pro_relation.getRelatStuId());
            List<Student> students = studentMapper.selectByExample(studentExample);

            departExample = new DepartExample();
            departExample.or().andIdEqualTo(students.get(0).getStuDepartId());
            List<Depart> departs = departMapper.selectByExample(departExample);
            int deptId = departs.get(0).getId();

            for (int i = 0; i < departList.size() - 1; i++) {
                if (departList.get(i).getId() == deptId) {
                    departList.remove(i);
                }
            }
        }
        for (Depart depart : departList) {
            resultMap = new HashMap<>();
            resultMap.put("deptId", depart.getId().toString());
            resultMap.put("deptName", depart.getDepName());
            resultList.add(resultMap);
        }
        //phyexamService.listLessRepeat(resultList);
        return JSON.toJSONString(resultList);
    }

    @Override
    public String findAllRegStu() {
        Stu_Pro_RelationExample stuProRelationExample = new Stu_Pro_RelationExample();
        List<Stu_Pro_Relation> stuProRelations = stuProRelationMapper.selectByExample(stuProRelationExample);

        Map<String, String> resultMap;
        List<Map<String, String>> resultList = new ArrayList<>();

        StudentExample studentExample;
        DepartExample departExample;
        ResultExample resultExample;
        for (Stu_Pro_Relation stuProRelation : stuProRelations) {
            resultMap = new HashMap<>();

            studentExample = new StudentExample();
            studentExample.or().andIdEqualTo(stuProRelation.getRelatStuId());
            List<Student> students = studentMapper.selectByExample(studentExample);
            resultMap.put("stuId", students.get(0).getId().toString());
            resultMap.put("stuName", students.get(0).getStuName());

            departExample = new DepartExample();
            departExample.or().andIdEqualTo(students.get(0).getStuDepartId());
            Depart depart = departMapper.selectByExample(departExample).get(0);
            resultMap.put("deptId", depart.getId().toString());
            resultMap.put("deptName", depart.getDepName());

            resultExample = new ResultExample();
            resultExample.or().andPhyRelationIdEqualTo(stuProRelation.getId());
            if (resultMapper.selectByExample(resultExample).size() == 0)
                resultMap.put("isPhy", "未体检");
            else
                resultMap.put("isPhy", "已体检");
            resultList.add(resultMap);
        }
        phyexamService.listLessRepeat(resultList);
        return JSON.toJSONString(resultList);
    }

    /**
     * @Description: 统计已经体检完成的学院信息：
     * 学院名称，学生个数=体检人数，合格人数
     **/
    @Override
    public String countDept() {
        ResultExample resultExample = new ResultExample();
        List<Result> results = resultMapper.selectByExample(resultExample);

        Map<String, String> resultMap;
        List<Map<String, String>> resultList = new ArrayList<>();

        Stu_Pro_RelationExample stuProRelationExample;
        StudentExample studentExample;
        DepartExample departExample;
        for (Result result : results) {
            resultMap = new HashMap<>();

            stuProRelationExample = new Stu_Pro_RelationExample();
            stuProRelationExample.createCriteria().andIdEqualTo(result.getPhyRelationId());
            Integer stuId = stuProRelationMapper.selectByExample(stuProRelationExample).get(0).getRelatStuId();

            studentExample = new StudentExample();
            studentExample.createCriteria().andIdEqualTo(stuId);
            Student student = studentMapper.selectByExample(studentExample).get(0);

            departExample = new DepartExample();
            departExample.createCriteria().andIdEqualTo(student.getStuDepartId());

            List<Depart> departs = departMapper.selectByExample(departExample);
            for (Depart depart : departs) {
                resultMap.put("deptName", depart.getDepName());
                studentExample = new StudentExample();
                studentExample.or().andStuDepartIdEqualTo(depart.getId());
                long count = studentMapper.countByExample(studentExample);
                resultMap.put("stuCount", Long.toString(count));
            }
            resultList.add(resultMap);
        }
        phyexamService.listLessRepeat(resultList);
        return JSON.toJSONString(resultList);
    }

    @Override
    public String countStudent() {
        ResultExample resultExample = new ResultExample();
        List<Result> results = resultMapper.selectByExample(resultExample);

        Map<String, String> resultMap;
        List<Map<String, String>> resultList = new ArrayList<>();

        Stu_Pro_RelationExample stu_pro_relationExample;
        StudentExample studentExample;
        for (Result result : results) {
            resultMap = new HashMap<>();
            stu_pro_relationExample = new Stu_Pro_RelationExample();
            studentExample = new StudentExample();

            stu_pro_relationExample.or().andIdEqualTo(result.getPhyRelationId());
            Integer stuId = stuProRelationMapper.selectByExample(stu_pro_relationExample).get(0).getRelatStuId();

            studentExample.createCriteria().andIdEqualTo(stuId);
            List<Student> students = studentMapper.selectByExample(studentExample);

            for (Student student : students) {
                // resultMap.put("dept",student.getStuDepartId().toString());
                resultMap.put("stuName", student.getStuName());
            }
            resultList.add(resultMap);
        }
        phyexamService.listLessRepeat(resultList);
        return JSON.toJSONString(resultList);
    }

    @Override
    public String findPhyByStuName(String stuName) {



        return null;
    }
}